package com.iflytek.day24;

import java.util.HashMap;
import java.util.Objects;
import java.util.Set;

/**
 * 学生类
 */
class Student {
  private String name;
  private int age;

  public Student() {
  }

  public Student(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Student student = (Student) o;
    return age == student.age && Objects.equals(name, student.name);
  }

  @Override
  public int hashCode() {
    return Objects.hash(name, age);
  }
}

/**
 * 测试类
 */
public class _06_MapDemo {
  public static void main(String[] args) {
    // 创建HashMap集合对象
    HashMap<Student, String> hm = new HashMap<Student, String>();

    // 创建学生对象
    Student s1 = new Student("林青霞", 30);
    Student s2 = new Student("张曼玉", 35);
    Student s3 = new Student("王祖贤", 33);
    Student s4 = new Student("王祖贤", 33);

    // 把学生添加到集合
    hm.put(s1, "西安");
    hm.put(s2, "武汉");
    hm.put(s3, "郑州");
    hm.put(s4, "北京");

    // 遍历集合
    Set<Student> keySet = hm.keySet();
    for (Student key : keySet) {
      String value = hm.get(key);
      System.out.println(key.getName() + "," + key.getAge() + "," + value);
    }
  }
}
